const { Op } = require("sequelize");

const brannerModel = appRequire('models/branner');

// 导出对象，供其它模块调用
module.exports = {
    list,
    save,
    destroy,
    changeOrder,
};

async function list(req, request) {
    let postData = req.body
    let returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let checkResult = Common.checkUrl(postData, [], request);
    if (checkResult == false) return;

    let params = {}
    let orders = [['createdAt', 'DESC']]
    let course = await brannerModel.branner.findAndCountAll({
        where: params,
        order: orders,
        include: ['artPPT'],
    })
    course = Common.clone(course)
    for (let item of course.rows) {
        if(item.artPPT){
            if (!item.image) {
                if (item.artPPT && item.artPPT.images && item.artPPT.images.length == 0) {
                    if (item.artPPT.type == "单个") {
                        item.image = item.artPPT.fileUrls[0].fileUrl + '?ci-process=doc-preview&dstType=png'
                    } else if (item.artPPT.type == "合集") {
                        item.image = item.artPPT.fileUrls[0].fileUrl + '?ci-process=doc-preview&dstType=png'
                    }
                }
            }
            item.title = item.artPPT.title
            item.artpptId = item.artPPT.objectId
        }
        
    }
    returnData.maxCount = course.count
    returnData.maxPage = Math.ceil(course.count / postData.pageSize)
    returnData.data = course.rows
    request.json(returnData)
}

async function save(req, request) {
    let postData = req.body
    let returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let checkResult = Common.checkUrl(postData, ['title'], request);
    if (checkResult == false) return;

    let params = {
        title: postData.title,
    }
    if (postData.artpptId) params.artpptId = postData.artpptId
    if (postData.image) params.image = postData.image
    if (postData.isShow !== undefined) params.isShow = postData.isShow
    if (postData.objectId) {
        let result = await brannerModel.branner.update(params, {
            where: {
                objectId: postData.objectId
            }
        })
        returnData.data = result
    } else {
        params.objectId = sid.uuid()
        let result = await brannerModel.branner.create(params)
        returnData.data = result
    }

    request.json(returnData)
}

async function destroy(req, request) {
    let postData = req.body
    let returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let checkResult = Common.checkUrl(postData, ['objectId'], request);
    if (checkResult == false) return;

    let result = await brannerModel.branner.destroy({
        where: {
            objectId: postData.objectId
        }
    })
    returnData.data = result
    request.json(returnData)
}

async function changeOrder(req, request) {
    let postData = req.body
    let returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let checkResult = Common.checkUrl(postData, [], request);
    if (checkResult == false) return;

    let banner1 = await brannerModel.branner.findOne({
        where: {
            objectId: postData.objectId1
        },
    })

    let banner2 = await brannerModel.branner.findOne({
        where: {
            objectId: postData.objectId2
        },
    })

    await brannerModel.branner.update({
        createdAt: banner2.createdAt
    }, {
        where: {
            objectId: postData.objectId1
        },
    })

    await brannerModel.branner.update({
        createdAt: banner1.createdAt
    }, {
        where: {
            objectId: postData.objectId2
        },
    })

    returnData.data = "成功"
    request.json(returnData)
}